package org.ueg.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.ueg.dao.interfaces.IDaoPessoa;
import org.ueg.exception.DaoException;
import org.ueg.model.Pessoa;

public class DaoPessoa extends Dao implements IDaoPessoa {

	@Override
	public void salva(Pessoa param) throws Exception {
		Session session = super.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.save(param);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			throw new DaoException(ex);
		} finally {
			session.close();
		}

	}

	@Override
	public void exclui(Pessoa param) throws Exception {
		Session session = super.getSession();
		Transaction tx = session.beginTransaction();
		try {
			session.delete(param);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			throw new DaoException(ex);
		} finally {
			session.close();
		}
	}

	@Override
	public Pessoa getById(Pessoa param) throws Exception {
		Session session = super.getSession();
		try {
			Pessoa pessoa = (Pessoa) session.get(Pessoa.class, param.getId());
			return pessoa;
		} finally {
			session.close();
		}
	}

	@Override
	public List<Pessoa> listaTudo() throws Exception {
		Session session = super.getSession();
		try {
			Query query = session.createQuery("from Pessoa");
			return query.list();
		} finally {
			session.close();
		}

	}
}
